﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp1
{
    /// <summary>
    /// 等待加密文件
    /// </summary>
    public class WaitEncryptionFile
    {
        [JsonProperty("awaitingEncryptionFileName")]
        public string AwaitingEncryptionFileName { get; set; }
        [JsonProperty("awaitingEncryptionFilePath")]
        public string awaitingEncryptionFilePath { get; set; }
        [JsonProperty("fileType")]
        public string fileType { get; set; }
    }
    /// <summary>
    /// 加密文件输出
    /// </summary>
    public class EncryptedFile
    {
        /// <summary>
        /// 加密文件名
        /// </summary>
        [JsonProperty("encryptedFileZipName")]
        public string EncryptedFileZipName { get; set; }
        /// <summary>
        /// 加密文件路径
        /// </summary>
        [JsonProperty("cryptedFileExportPath")]
        public string CryptedFileExportPath { get; set; }
    }
    public class CaShareEncryptTool
    {
        public async Task WebSocket(List<WaitEncryptionFile> waitEncryptionFiles, EncryptedFile encryptedFile, CaShareConfig caShareConfig)
        {
            try
            {
                //请求数据封装
                var msgJson = new
                {
                    CAShareUseScene = "01",
                    keyParameter = new
                    {
                        awaitingEncryptionFileList = waitEncryptionFiles,
                        encryptedFileZipName = encryptedFile.EncryptedFileZipName,
                        cryptedFileExportPath = encryptedFile.CryptedFileExportPath,
                        caShareComponentConfig = caShareConfig
                    }
                };
                var msgStr = JsonConvert.SerializeObject(msgJson);

               // msgStr = "{\"CAShareUseScene\":\"01\",\"keyParameter\":{\"awaitingEncryptionFileList\":[{\"awaitingEncryptionFilePath\":\"c:\\\\test.pdf\",\"awaitingEncryptionFileName\":\"test.pdf\",\"fileType\":\"pdf\"}],\"encryptedFileZipName\":\"test.zip\",\"encryptedFileExportPath\":\"d:/demo/\",\"caShareComponentConfig\":{\"tradingSystemCode\":\"X6200005612\",\"caShareComponentServiceUrl\":\"http://36.142.16.73:8992\",\"clientId\":\"Z0010020035\",\"clientSecret\":\"DA960F1BC072486BBC3C04023C0743A5\",\"signatureSecret\":\"54ECD15D6E0440E3AF79AB3D3DACC6F8\"}}}";
                ClientWebSocket webSocket = new ClientWebSocket();
                CancellationToken cancellation = new CancellationToken();
                var url = "ws://localhost:12345";
                await webSocket.ConnectAsync(new Uri(url), cancellation);
                var bsend = Encoding.UTF8.GetBytes(msgStr);
                await webSocket.SendAsync(bsend, WebSocketMessageType.Text, true, cancellation);

            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }
        /// <summary>
        /// 启动ca互认加密工具
        /// </summary>
        public void Start()
        {
            Process process = new Process();
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.Arguments = "CAShareDetectInstallTool://";
            process.StartInfo.FileName = @"explorer";
            process.Start();
            Task.Delay(TimeSpan.FromSeconds(2));
        }
    }
}
